<template>
  <view class="password">
    <u-form class="password-form" :model="form" ref="form" :error-type="['toast']">
      <u-form-item class="password-form_item" prop="email" left-icon="email" :left-icon-style="iconStyle">
        <u-input v-model="form.email" disabled :placeholder="i18n.input_email"/>
      </u-form-item>
      <u-form-item class="password-form_item" prop="password" left-icon="lock" :left-icon-style="iconStyle">
        <u-input v-model="form.password" type="password" :placeholder="i18n.inut_new_pwd"/>
      </u-form-item>
      <u-form-item class="password-form_item" prop="npassword" left-icon="lock" :left-icon-style="iconStyle">
        <u-input v-model="form.npassword" type="password" :placeholder="i18n.inut_re_pwd"/>
      </u-form-item>
    </u-form>
    <view class="password-button">
      <u-button type="primary" @click="submit">{{i18n.btn_save}}</u-button>
    </view>
  </view>
</template>

<script>
import pageMixin from '@/utils/pageMixin';
export default {
  mixins: [pageMixin],
  data() {
    return {
      form: {
        email: '',
        password: '',
        npassword: '',
      },
      iconStyle: {
        fontSize: '38rpx',
        color: '#646466',
        display: 'flex'
      },
      rules: {
        email: [
          {
            required: true,
            message: '邮箱格式错误',
            type: 'email'
          }
        ],
        password: [
          {
            min: 6,
            max: 18,
            message: '请输入6-18位新密码'
          }
        ],
        npassword: [
          {
            min: 6,
            max: 18,
            message: '请输入6-18位新密码'
          }
        ]
      }
    }
  },

  onLoad(options) {
      this.form.email = options.loginId;
  },
  onShow() {
  },
  onReady() {
    this.$refs.form.setRules(this.rules);
  },
  components: {},
  computed:{
  	i18n () {  
  		return this.$t('mine')  
  	}
  },
  methods: {
    submit() {
      this.$refs.form.validate(valid => {
        if (valid) {
            if (this.form.password != this.form.npassword){
                uni.showToast({
                    title: '两次输入密码不一致',
                    icon: 'none'
                });
                return;
            }
          this.$http.post('/client/auth/account/reset', {email: this.form.email, password: this.form.password}).then(res => {
                uni.showToast({
                    title: '修改成功',
                    duration: 1500
                });
                setTimeout(() => {
                    this.$u.route('/pages/login/index');
                }, 1500)
          });
        } else {
          console.log('验证失败');
        }
      });
    }
  }
}
</script>

<style lang="scss" scoped>
.password {
  height: 100vh;
  background-color: #ffffff;
}

.password-form {
  padding: 0 30rpx;
  margin-bottom: 60rpx;

  &_item {
    // margin-bottom: 30rpx;
  }
}

.password-button {
  padding: 0 80rpx;
}
</style>
